package org.rc.model;

import com.google.gson.annotations.Expose;
import com.googlecode.objectify.Key;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Id;

public class Race implements Serializable {

    public static final double METERS_IN_MILE = 1609.344;
    private static final long serialVersionUID = 1L;
    @Expose
    @Id
    private Long id;
    @Expose
    private String name;
    private Date date;
    private Double distanceM;
    private Key<ResultsFile> importedBy;
    private Date created = new Date();
    private Integer numberOfResults;

    /**
     * Returns true if the name and data match
     * @param obj
     * @return 
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Race other = (Race) obj;
        if (name!=null && !name.equals(other.name)) {
            return false;
        }
        if (date==null && other.getDate()!=null) {
            return false;
        }
        if(date!=null && !date.equals(other.getDate())) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return (name + date).hashCode();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * @return the distanceM. 
     * The distance in Meters
     */
    public Double getDistanceM() {
        return distanceM;
    }

    /**
     * @param distanceM the distanceM to set
     * The distance in Meters
     */
    public void setDistanceM(Double distanceM) {
        this.distanceM = distanceM;
    }

    /**
     * @return the distaance in Miles
     */
    public double getMiles() {
        if (distanceM != null) {
            return distanceM / METERS_IN_MILE;
        } else {
            return 0;
        }
    }

    /**
     * @param distanceM the distanceM to set
     */
    public void setMiles(double miles) {
        this.distanceM = miles * METERS_IN_MILE;
    }

    /**
     * @return the distanceM
     */
    public double getKM() {
        if (distanceM != null) {
            return distanceM / 1000;
        } else {
            return 0;
        }
    }

    /**
     * @param distanceM the distanceM to set
     */
    public void setKM(double km) {
        this.distanceM = km * 1000;
    }

    /**
     * @return the created
     */
    public Date getCreated() {
        return created;
    }

    /**
     * @param created the created to set
     */
    public void setCreated(Date created) {
        this.created = created;
    }

    /**
     * @return the importedBy
     */
    public Key<ResultsFile> getImportedBy() {
        return importedBy;
    }

    /**
     * @param importedBy the importedBy to set
     */
    public void setImportedBy(Key<ResultsFile> importedBy) {
        this.importedBy = importedBy;
    }

    /**
     * This is intended to be populated either at upload/import of results or later by a task/map-reduce job
     * Not guaranteed to be the correct value
     * @return the numberOfResults
    */
    public Integer getNumberOfResults() {
        return numberOfResults;
    }

    /**
     * This is intended to be populated either at upload/import of results or later by a task/map-reduce job
     * @param numberOfResults the numberOfResults to set
     */
    public void setNumberOfResults(Integer numberOfResults) {
        this.numberOfResults = numberOfResults;
    }
}
